import {MethodMapping} from '@/app/base/action/annotation/ActionDecorator';
import AbstractMaterial from '@/app/base/context/AbstractMaterial';
import UserChatService from '@/app/com/main/module/business/chat/service/UserChatService';
import Content from '@/app/com/common/chat/Content';
import BaseContentItemUtil from '@/app/com/common/chat/util/BaseContentItemUtil';
import PersonalBox from "@/app/com/main/module/business/personal/box/PersonalBox";
import UserChatSender from "@/app/com/main/module/business/chat/sender/UserChatSender";
import {EventBus} from '@/common/vue/event-bus.js';

export default class UserChatAction extends AbstractMaterial {
    private static action: string = '2.2.001';

    @MethodMapping(UserChatAction, UserChatAction.action, '1.2.0001')
    public update(data: any): void {
        if (data && data.body) {
            const sendUserId: string = data.body.sendUserId;
            const receiveUserId: string = data.body.receiveUserId;
            const content: Content = data.body.content;
            const ucs: UserChatService = this.appContext.getMaterial(UserChatService);
            ucs.chat(true, sendUserId, receiveUserId, content);
        }
    }


    @MethodMapping(UserChatAction, UserChatAction.action, '1.2.0004')
    public callVideo(data: any): void {
        if (data && data.body) {
            let receiveUserId = data.body.receiveUserId;
            let sendUserId = data.body.sendUserId;
            let sendUserName = data.body.sendUserName;
            console.log("接收服务端消息：");
            console.log(data);

            const pb: PersonalBox = this.appContext.getMaterial(PersonalBox);

            if(pb.getUser().id!=receiveUserId){
                return;
            }

            if(confirm("【"+sendUserName+"】发来视频,是否接受")){
                //todo 告知服务端 我已经准备好
                console.log("告知服务端 我已经准备好")
                this.sendReceiveInfo(receiveUserId,sendUserId,true);
            }else{
                //todo  告知服务端 我已经拒绝
                console.log("告知服务端 我已经拒绝");
                this.sendReceiveInfo(receiveUserId,sendUserId,false);
            }
        }
    }

    @MethodMapping(UserChatAction, UserChatAction.action, '1.2.0005')
    public conneVideo(data: any): void {
        if (data && data.body) {
            const ids: string[] = data.body.ids;
            const roomId: string = data.body.roomId;
            const pb: PersonalBox = this.appContext.getMaterial(PersonalBox);
            const currentUserId = pb.getUserId();
            console.log("ids:"+ids+", currentUserId:"+currentUserId+", is:"+ids.indexOf(currentUserId));
            if(ids.indexOf(currentUserId)>=0){
                if(roomId==null || roomId==undefined){
                    alert("用户已拒绝视频");
                }else{
                    console.log("显示视频");
                    EventBus.$emit('video', roomId);
                }
            }
        }
    }


    public sendReceiveInfo(sendUserId:string,receiveUserId:string,isReceive:boolean){
        const userChatSender: UserChatSender = this.appContext.getMaterial(UserChatSender);

        userChatSender.sendReceiveInfo(sendUserId,receiveUserId,isReceive);
    }


}
